package com.example.seakdreamandroid.cardQuest;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.example.seakdreamandroid.AppIntroActivity;
import com.example.seakdreamandroid.Constant;
import com.example.seakdreamandroid.LoginActivity;
import com.example.seakdreamandroid.MainActivity;
import com.example.seakdreamandroid.R;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

class MyAdapter extends RecyclerView.Adapter<MyAdapter.PagerViewHolder>  {
    private List<SwipeCardBean> mList;
    private String[] point = new String[10];
    private ViewPager2 viewPager2;
    private CardQuestionActivity cardQuestionActivity;
    private AssessResultActivity assessResultActivity;

    public AssessResultActivity getAssessResultActivity() {
        return assessResultActivity;
    }

    public void setAssessResultActivity(AssessResultActivity assessResultActivity) {
        this.assessResultActivity = assessResultActivity;
    }

    public CardQuestionActivity getCardQuestionActivity() {
        return cardQuestionActivity;
    }

    public void setCardQuestionActivity(CardQuestionActivity cardQuestionActivity) {
        this.cardQuestionActivity = cardQuestionActivity;
    }

    public List<SwipeCardBean> getmList() {
        return mList;
    }

    public void setmList(List<SwipeCardBean> mList) {
        this.mList = mList;
    }

    public String[] getPoint() {
        return point;
    }

    public void setPoint(String[] point) {
        this.point = point;
    }

    public ViewPager2 getViewPager2() {
        return viewPager2;
    }

    public void setViewPager2(ViewPager2 viewPager2) {
        this.viewPager2 = viewPager2;
    }

    public PagerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_page, parent, false);
        return new PagerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PagerViewHolder holder, int position) {
        holder.bindData(position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setList(List<SwipeCardBean> list){
        this.mList = list;
    }

    class PagerViewHolder extends RecyclerView.ViewHolder {
        public TextView question = itemView.findViewById(R.id.question);
        public Button selectA= itemView.findViewById(R.id.selectA);
        public Button selectB= itemView.findViewById(R.id.selectB);
        public Button selectC= itemView.findViewById(R.id.selectC);
        public Button selectD= itemView.findViewById(R.id.selectD);
        public Button selectE= itemView.findViewById(R.id.selectE);
        public TextView number= itemView.findViewById(R.id.number);
        public Button next = itemView.findViewById(R.id.next);
        public RadioGroup radioGroup = itemView.findViewById(R.id.radio_group);

        public PagerViewHolder(@NonNull View itemView) {
            super(itemView);
        }
//
//        public PagerViewHolder(@NonNull View itemView) {
//            super(itemView);
//        }

        private void bindData(int index) {
            question.setText(mList.get(index).getTitle());
            selectA.setText(mList.get(index).getSelectA());
            selectB.setText(mList.get(index).getSelectB());
            selectC.setText(mList.get(index).getSelectC());
            selectD.setText(mList.get(index).getSelectD());
            selectE.setText(mList.get(index).getSelectE());
            number.setText(mList.get(index).getNumber());

            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    RadioButton radioButton  = group.findViewById(checkedId);
                    if(!mList.get(index).getNumber().equals("10/10")){
                        next.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mList.get(index).setSelect(radioButton.getText().subSequence(0,1).toString());
                                viewPager2.beginFakeDrag();
                                viewPager2.fakeDragBy(-600f);
                                viewPager2.endFakeDrag();
                            }
                        });
                    }else {
                        next.setText("完成");
                        next.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mList.get(index).setSelect(radioButton.getText().subSequence(0,1).toString());
                                System.out.println("结果是：");
                                int amount = 0;
                                for(int i=0;i<mList.size();i++){
                                    switch (mList.get(i).getSelect()){
                                        case "A":
                                            amount = amount + 2;
                                            break;
                                        case "B":
                                            amount = amount + 4;
                                            break;
                                        case "C":
                                            amount = amount + 6;
                                            break;
                                        case "D":
                                            amount = amount + 8;
                                            break;
                                        case "E":
                                            amount = amount + 10;
                                            break;
                                    }
                                }
                                System.out.println(amount);
                                getView(String.valueOf(amount));
                            }
                        });
                    }


                }
            });


        }


    }

    private void getView(String amount){
        try {
            OkHttpClient okHttpClient = new OkHttpClient();
            RequestBody requestBody = new FormBody.Builder()
                    .add("amount", amount)
                    .build();
            Request request = new Request.Builder()
                    .url(Constant.BASE_URL + "assessResult/getAssessResult")
                    .post(requestBody)
                    .build();
            Call call = okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    Log.i("okhttp", "请求失败");
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String str = response.body().string();
                    try {
                        JSONObject result = new JSONObject(str);
                        if (result.getString("code").equals("404")){
                            cardQuestionActivity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    System.out.println("aaa");
                                    //显示
                                    Toast.makeText(cardQuestionActivity,"测试失败",Toast.LENGTH_SHORT).show();
                                }
                            });
                        }else if (result.getString("code").equals("200")){
                            JSONObject assess = result.getJSONObject("data");
                            System.out.println(assess);
//                            Log.i("adadad", result.getString("token"));
//                            editor.putString("token", result.getString("token"));
//                            editor.commit();
                            Intent intent = new Intent(cardQuestionActivity, AssessResultActivity.class);
                            intent.putExtra("count",amount);
                            intent.putExtra("advice",assess.getString("advice"));
                            cardQuestionActivity.startActivity(intent);
                            cardQuestionActivity.finish();
//                            user.setUserId(userInfo.getString("userId"));
//                            user.setIf_vip(userInfo.getString("ifVip"));
//                            startActivity(intent);
//                            if (password.equals(userInfo.getString("password"))){
//                                Intent intent = new Intent();
//                                intent.setClass(LoginActivity.this, MainActivity.class);
////                                intent.putExtra("userId", userInfo.getString("userId"));
//                                user.setUserId(userInfo.getString("userId"));
//                                startActivity(intent);
//                            }else{
//                                Toast.makeText(LoginActivity.this,"密码错误",Toast.LENGTH_SHORT).show();
//                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}